// 复习页面JavaScript功能

// 显示词汇详情
function showWordDetails(word) {
    // 显示加载动画
    const loadingSpinner = document.querySelector('.loading-spinner');
    const wordDetails = document.querySelector('.word-details');
    
    loadingSpinner.classList.remove('hidden');
    wordDetails.classList.add('hidden');
    
    // 模拟加载延迟
    setTimeout(() => {
        loadWordContent(word);
        loadingSpinner.classList.add('hidden');
        wordDetails.classList.remove('hidden');
    }, 1000);
}

// 加载词汇内容
function loadWordContent(word) {
    const wordData = wordsData[word];
    if (!wordData) return;
    
    const wordDetails = document.querySelector('.word-details');
    
    let html = `
        <div class="word-header">
            <h2>${word}</h2>
            <button class="close-btn" onclick="hideWordDetails()">×</button>
        </div>
        <div class="meanings">
    `;
    
    wordData.meanings.forEach((meaning, meaningIndex) => {
        html += `
            <div class="meaning-item">
                <h3>${meaningIndex + 1}. ${meaning.meaning}</h3>
                <div class="examples">
        `;
        
        meaning.examples.forEach((example, exampleIndex) => {
            const exampleId = `example-${meaningIndex}-${exampleIndex}`;
            const translationId = `translation-${meaningIndex}-${exampleIndex}`;
            
            html += `
                <div class="example">
                    <div class="example-text">${example.text.replace(new RegExp(word, 'g'), `<span class="highlight">${word}</span>`)}</div>
                    <div class="example-source">——${example.source}</div>
                    <div class="example-actions">
                        <button class="translation-btn" onclick="toggleTranslation('${translationId}', this)">
                            📖 显示翻译
                        </button>
                    <button class="example-ask-ai" onclick="askAI('${word}', '${meaning.meaning}', '${example.text}')">
                        🤖 询问AI
                    </button>
                    </div>
                    <div class="example-translation hidden" id="${translationId}">
                        <div class="translation-content">
                            ${example.translation}
                        </div>
                    </div>
                </div>
            `;
        });
        
        html += `
                </div>
            </div>
        `;
    });
    
    html += '</div>';
    wordDetails.innerHTML = html;
}

// 切换翻译显示/隐藏
function toggleTranslation(translationId, button) {
    const translationDiv = document.getElementById(translationId);
    
    if (translationDiv.classList.contains('hidden')) {
        // 显示翻译
        translationDiv.classList.remove('hidden');
        button.innerHTML = '📖 隐藏翻译';
        button.classList.add('active');
    } else {
        // 隐藏翻译
        translationDiv.classList.add('hidden');
        button.innerHTML = '📖 显示翻译';
        button.classList.remove('active');
    }
}

// 隐藏词汇详情
function hideWordDetails() {
    const wordDetails = document.querySelector('.word-details');
    wordDetails.classList.add('hidden');
}

// AI询问功能（简化版）
function askAI(word, meaning, example) {
    alert(`AI助手提示：\n\n词汇：${word}\n释义：${meaning}\n\n例句分析：\n${example}\n\n这个例句中"${word}"的用法体现了"${meaning}"的含义。您可以通过上下文来理解这个词在古文中的具体用法。`);
}

// 返回主页
function goHome() {
    window.location.href = '../index.html';
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
    // 确保所有元素初始状态正确
    const loadingSpinner = document.querySelector('.loading-spinner');
    const wordDetails = document.querySelector('.word-details');
    
    if (loadingSpinner) {
        loadingSpinner.classList.add('hidden');
    }
    if (wordDetails) {
        wordDetails.classList.add('hidden');
    }
});